package com.example.demo.leetcode;

/**
 * https://leetcode.cn/problems/perfect-squares/description/?envType=study-plan-v2&envId=top-100-liked
 *
 * @author WangYX
 * @version 1.0.0
 * @date 2024/03/14 10:55
 */
public class _279_完全平方数 {

    public static void main(String[] args) {
        int i = numSquares(48);

        System.out.println("========");
        System.out.println(i);
    }

    public static int numSquares(int n) {
        int sqrt = sqrt(n);
        int num = Integer.MAX_VALUE;
        for (int i = sqrt; i >= 1; i--) {
            System.out.println();
            num = Math.min(num, temp(i, n));
        }
        System.out.println();
        return num;
    }

    public static int sqrt(int n) {
        return Double.valueOf(Math.sqrt(n)).intValue();
    }

    public static int temp(int x, int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1) {
            System.out.print(1 + ",");
            return 1;
        }
        int num = 0;

        int v = Double.valueOf(Math.pow(x, 2)).intValue();
        System.out.print(v + ",");
        if (v == n) {
            num += 1;
        } else {
            num += 1;
            int sqrt = sqrt(n - v);
            num += temp(sqrt, n - v);
        }
        return num;
    }



    public static int numSquares1(int n) {
        int[] f = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            int minn = Integer.MAX_VALUE;
            for (int j = 1; j * j <= i; j++) {
                minn = Math.min(minn, f[i - j * j]);
            }
            f[i] = minn + 1;
        }
        return f[n];
    }
}
